<?php
class user{
    private $db;
    public function conndb(){
        $this->db=new mysqli("192.168.254.108","firadio_account","123","firadio_account",3306);
    }
    /*
    方法名：exist()
    功能：检测用户是否存在，存在返回1，不存在返回2，错误返回-1
    */
    public function exist($name){
        $sql="SELECT id FROM user_pass WHERE name='$name'";
        $r=$this->db->query($sql);
        if(!$r){
            echo '查询失败！['.$db->errno.']'.$db->error;
            return(-1);
        }
        if($r->fetch_assoc()){
            return(1);
        }else{
            return(2);
        }
    }
    public function add($name,$password,$gender,$birthdate){
        $begintime=microtime(true);
        $beginipv4=$_SERVER['REMOTE_ADDR'];
        $this->db->autocommit(FALSE);
        $this->db->begin_transaction();
        $sql="INSERT INTO user_pass(begintime,beginipv4,`name`,password)VALUES('$begintime','$beginipv4','$name','$password')";
        if(!$this->db->query($sql)){
            //当查询失败时，回滚。
            $this->db->rollback();
            return(-1.1);
        }
        $user_pass_id=$this->db->insert_id;
        $sql="INSERT INTO user_info(user_pass_id,gender,birthdate)VALUES('$user_pass_id','$gender','$birthdate')";
        if(!$this->db->query($sql)){
            $this->db->rollback();
            return(-1.2);
        }
        if($this->db->commit()){
            return(1);
        }
        return(0);
    }
}
?>